package System_code;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2022-08-10
 * Time: 17:22
 * 死锁
 */
public class Demo28 {
    public static Object locker1=new Object();
    public static Object locker2=new Object();

    public static void main(String[] args) {
        Thread t1=new Thread(()->{
            synchronized (locker1){

                try {
                    System.out.println("你给我筷子，我就给你叉子");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (locker2){
                    System.out.println("交换成功");
                }
            }
        });
        t1.start();
        Thread t2=new Thread(()->{
           synchronized (locker2){

               try {
                   System.out.println("你给我叉子，我就给你筷子");
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               synchronized (locker1){
                   System.out.println("交换成功");
               }
           }
        });
        t2.start();
    }
}
